package com.hucii.app.activity;

import java.util.Observable;
import java.util.Observer;

import com.hucii.app.MyApplication;
import com.hucii.app.R;
import com.hucii.app.utils.Utils;
import com.hucii.app.utils.UtilsLog;

import android.app.Dialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;

public class MainTabActivity extends TabActivity implements CompoundButton.OnCheckedChangeListener, OnDismissListener {

	private TabHost host;
	private RadioButton[] radioButtons;
	private MyApplication mApp;
	private Context mContext;
	private boolean firstCreate, exit;
	private int oldSwitchId = -1;
	private Animation animationRightIn, animationRightOut, animationLeftIn, animationLeftOut;

	public static final String TAB_PRE = "tab_pre";
	public static final String TAB_HOME = "tab_home";
	public static final String TAB_FAVORITES = "tab_favorites";
	public static final String TAB_EDIT_ORDER = "tab_edit_order";
	public static final String TAB_STATEMENT = "TAB_statement";
	public static final String TAB_ORDER_BY_PIC = "tab_order_by_pic";
	public static final String TAB_MUST_ORDER = "tab_must_order";
	public static final String TAB_CANCEL_ORDER = "tab_cancel_order";
	public static final String TAB_NEXT = "tab_next";

	public static boolean from;
	// 是否是筛选进入的列表或地图
	public static boolean isSift = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		exit = false;
		mApp = MyApplication.getSelf();
		// 添加观察者
		firstCreate = true;
		setContentView(R.layout.main_tab);

		from = getIntent().getBooleanExtra("isRequest", false);

		host = getTabHost();
		host.addTab(host.newTabSpec(TAB_PRE).setIndicator(TAB_PRE).setContent(new Intent(this, PicViewActivity.class)));
		host.addTab(host.newTabSpec(TAB_HOME).setIndicator(TAB_HOME).setContent(new Intent(this, PicViewActivity.class)));
		host.addTab(host.newTabSpec(TAB_FAVORITES).setIndicator(TAB_FAVORITES).setContent(new Intent(this, FavoritesActivity.class)));
		host.addTab(host.newTabSpec(TAB_EDIT_ORDER).setIndicator(TAB_EDIT_ORDER).setContent(new Intent(this, OrderEditorActivity.class)));
		host.addTab(host.newTabSpec(TAB_STATEMENT).setIndicator(TAB_STATEMENT).setContent(new Intent(this, OrderStatisticalTableActivity.class)));
		host.addTab(host.newTabSpec(TAB_ORDER_BY_PIC).setIndicator(TAB_ORDER_BY_PIC).setContent(new Intent(this, OrderByPictureActivity.class)));
		host.addTab(host.newTabSpec(TAB_MUST_ORDER).setIndicator(TAB_MUST_ORDER).setContent(new Intent(this, MustGoodsStyleActivity.class)));
		host.addTab(host.newTabSpec(TAB_CANCEL_ORDER).setIndicator(TAB_CANCEL_ORDER).setContent(new Intent(this, CancelOrderActivity.class)));
		host.addTab(host.newTabSpec(TAB_NEXT).setIndicator(TAB_NEXT).setContent(new Intent(this, PicViewActivity.class)));

		RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
		radioButtons = new RadioButton[9];
		for (int i = 0; i < 9; i++) {
			radioButtons[i] = (RadioButton) radioGroup.findViewWithTag("rb_" + i);
			radioButtons[i].setOnCheckedChangeListener(this);
		}

		int switchid = getIntent().getIntExtra("switchid", 1);

		radioButtons[switchid].setChecked(true);
		animationRightIn = AnimationUtils.loadAnimation(this, R.anim.push_right_in);
		animationRightOut = AnimationUtils.loadAnimation(this, R.anim.push_right_out);
		animationLeftIn = AnimationUtils.loadAnimation(this, R.anim.push_left_in);
		animationLeftOut = AnimationUtils.loadAnimation(this, R.anim.push_left_out);
	}

	public void switchTab(int switchid) {
		radioButtons[switchid].setChecked(true);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (event.getRepeatCount() == 0) {
				if (oldSwitchId == 0) {
					showDialog(0);
				} else {
					radioButtons[0].setChecked(true);
				}
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

		if (isChecked) {
			for (int i = 0; i < radioButtons.length; i++) {

				if (buttonView == radioButtons[i]) {

					UtilsLog.i("msg", "index:" + i);

					if (buttonView == radioButtons[0] && oldSwitchId == 1) {
						
						((PicViewActivity)getCurrentActivity()).onPrePageClick();
						// 回调
						UtilsLog.i("msg", "pre");
						// Utils.toast(mContext, "pre");
						return;

					} else if (buttonView == radioButtons[8] && oldSwitchId == 1) {
						// 回调
						UtilsLog.i("msg", "next");
						// Utils.toast(mContext, "next");
						((PicViewActivity)getCurrentActivity()).onNextPageClick();
						return;

					} else {
						if (firstCreate) {
							radioButtons[i].setTextColor(getResources().getColor(R.color.black));
							host.setCurrentTab(i);
						} else {
							if (oldSwitchId > i) {
								host.getCurrentView().startAnimation(animationRightOut);
								radioButtons[i].setTextColor(getResources().getColor(R.color.black));
								host.setCurrentTab(i);
								host.getCurrentView().startAnimation(animationRightIn);
							} else if (oldSwitchId < i) {
								host.getCurrentView().startAnimation(animationLeftOut);
								radioButtons[i].setTextColor(getResources().getColor(R.color.black));
								host.setCurrentTab(i);
								host.getCurrentView().startAnimation(animationLeftIn);
							}
						}
						firstCreate = false;
						oldSwitchId = i;
					}
					if (i == 1) {
						radioButtons[0].setVisibility(View.VISIBLE);
						radioButtons[8].setVisibility(View.VISIBLE);
					} else {
						radioButtons[0].setVisibility(View.GONE);
						radioButtons[8].setVisibility(View.GONE);
					}

				} else {
					if (radioButtons[i] != null)
						radioButtons[i].setTextColor(getResources().getColor(R.color.black));
				}
			}
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {

		return null;
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		if (exit) {

		}
	}

}
